package com.softstao.guoyu.ui.activity.agent;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gigamole.library.navigationtabstrip.NavigationTabStrip;
import com.softstao.guoyu.R;
import com.softstao.guoyu.base.BaseActivity;
import com.softstao.guoyu.global.SharePreferenceManager;
import com.softstao.guoyu.model.agent.SubApply;
import com.softstao.guoyu.model.agent.SubApplyList;
import com.softstao.guoyu.mvp.interactor.agent.AgentInteractor;
import com.softstao.guoyu.mvp.presenter.agent.AgentPresenter;
import com.softstao.guoyu.mvp.viewer.agent.SubApplyViewer;
import com.softstao.guoyu.ui.activity.me.AuditDetailActivity;
import com.softstao.guoyu.ui.baseAdapter.RecycleViewBaseAdapter;
import com.softstao.guoyu.ui.baseAdapter.RecycleViewHolder;
import com.softstao.softstaolibrary.library.widget.CustomScrollerView;
import com.softstao.softstaolibrary.library.widget.CustomTabStrip;
import com.softstao.softstaolibrary.library.widget.EmptyLayout;
import com.softstao.softstaolibrary.library.widget.FullyLinearLayoutManager;
import com.softstao.softstaolibrary.library.widget.MarginDecoration3;
import com.wangjie.androidinject.annotation.annotations.mvp.AIPresenter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;

/**
 * Created by vaikus on 2017/6/23.
 */

public class AuditListActivity extends BaseActivity implements SubApplyViewer{

    @AIPresenter(presenter = AgentPresenter.class,interactor = AgentInteractor.class)
    AgentPresenter presenter;

    @BindView(R.id.tab_strip)
    CustomTabStrip tabStrip;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.more)
    TextView more;
    @BindView(R.id.scroll_view)
    CustomScrollerView scrollView;
    @BindView(R.id.empty_layout)
    EmptyLayout emptyLayout;
    @BindView(R.id.loading)
    RelativeLayout loading;
    private int state = 1;
    private List<SubApply> data = new ArrayList<>();
    private RecycleViewBaseAdapter<SubApply> adapter;
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public int _ContentView() {
        return R.layout.activity_audit_list;
    }

    @Override
    public void initView() {
        initTitle("授权审核");
        scrollView.setOnScrollChangedListener(this);
        setPtrFrameLayoutEnable();

        tabStrip.setTitles("待处理", "已通过", "已驳回");
        tabStrip.setTabIndex(0);
        tabStrip.setOnTabStripSelectedIndexListener(new NavigationTabStrip.OnTabStripSelectedIndexListener() {
            @Override
            public void onStartTabSelected(String title, int index) {
            }

            @Override
            public void onEndTabSelected(String title, int index) {
                switch (title){
                    case "待处理":
                        state = 1;
                        break;
                    case "已通过":
                        state = 2;
                        break;
                    case "已驳回":
                        state = 3;
                        break;
                }
                currentPage = 1;
                getSubApply();
            }
        });

        adapter = new RecycleViewBaseAdapter<SubApply>(data,R.layout.layout_sub_apply_item) {
            @Override
            public void convert(RecycleViewHolder holder, SubApply subApply) {
                RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                holder.itemView.setLayoutParams(layoutParams);
                String status = null;
                switch (subApply.getState()){
                    case 1:
                        status = "待处理";
                        break;
                    case 2:
                        status = "已通过";
                        break;
                    case 3:
                        status = "已驳回";
                        break;
                }
                holder.setText(R.id.title,subApply.getApplyName())
                        .setText(R.id.apply_name,"申请人："+subApply.getApplyAgent())
                        .setText(R.id.time,format.format(new Date(subApply.getDate())))
                        .setText(R.id.status,status);
            }
        };
        adapter.setListener(position -> {
            Intent intent = new Intent(context,AuditDetailActivity.class);
            intent.putExtra("type","agent");
            intent.putExtra("id",data.get(position).getProcessId());
            intent.putExtra("status",data.get(position).getState());
            startActivity(intent);
        });
        list.setAdapter(adapter);
        list.setLayoutManager(new FullyLinearLayoutManager(this));
        list.addItemDecoration(new MarginDecoration3(this));
    }

    @Override
    protected void onResume() {
        super.onResume();
        getSubApply();
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        getSubApply();
    }

    @Override
    public void onLoad() {
        super.onLoad();
        getSubApply();
    }

    @Override
    public void getSubApply() {
        loading.setVisibility(View.VISIBLE);
        presenter.getSubApply(currentPage, SharePreferenceManager.getInstance().getAgentId(),state);
//        presenter.getSubApply(currentPage,5,state);
    }

    @Override
    public void getList(SubApplyList subApplyList) {
        loading.setVisibility(View.GONE);
        if (subApplyList!=null){
            if (subApplyList.getApplyList()!=null&&subApplyList.getApplyList().size()!=0){
                emptyLayout.setVisibility(View.GONE);
                more.setVisibility(View.GONE);
                if (currentPage==1){
                    data.clear();
                }
                data.addAll(subApplyList.getApplyList());
                adapter.notifyDataSetChanged();
            }else {
                if (currentPage==1){
                    emptyLayout.setVisibility(View.VISIBLE);
                }else {
                    more.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    @Override
    protected void error(String msg) {
        super.error(msg);
        loading.setVisibility(View.GONE);
    }
}
